package com.chenggcode.security.jwt.repository;

import com.chenggcode.security.jwt.entity.UserInfo;
import org.hibernate.annotations.UpdateTimestamp;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Optional;

/**
 * @author Chenggcode
 * @date 2025/7/7 15:21
 */
@Repository
public interface UserInfoRepository extends JpaRepository<UserInfo, Integer> {
    Optional<UserInfo> findByUsername(String username);

    @Transactional(rollbackFor = Exception.class)
    @Modifying
    @Query("UPDATE tb_user_info u SET u.refreshToken = ?2, u.refreshTokenExpiration = ?3 WHERE u.username = ?1")
    void updateRefreshToken(String username, String refreshToken, LocalDateTime refreshTokenExpiration);

    UserInfo getByRefreshToken(String refreshToken);
}
